const requestUtil = {
    /**
     * 报文封装
     * @param {Form} 初级参数
     * @returns 报文封装好的
     */
    paramEnc:(Form,obj) => {
        
        
        let{custId} = obj;
        let newForm = {};
        for(var key in Form){
            let item = Form[key],
                newItem = {custId:custId},
                tableName = '';
                
            if(!item.hasOwnProperty("isSave")){
                item.isSave = true;
            }
            if(item.tableName && item.isSave){

                tableName = requestUtil.toHump(item.tableName)+'s';
                if(item.colType=='C'){
                    // 横表
                    let colId = requestUtil.toHump(item.colId);
                    switch (tableName){
                        case 'orderTasks'||'ORDER_TASK':
                            if(item.value==""||item.value==undefined){
                                break;
                            }
                            newItem[colId] = item.value+'';
                            newForm=requestUtil.buildOrderTasks(tableName,newForm,newItem,obj);
                            break;
                        case 'orderItems' || 'ORDER_ITEM':
                            newItem[colId] = item.value+'';
                            newForm=requestUtil.buildOrderItems(tableName,newForm,newItem,obj);
                            break;
                        case 'acctItemOrgs' || 'ACCT_ITEM_ORG':
                            newItem[colId] = item.value+'';
                            newForm=requestUtil.buildAcctItemOrgs(tableName,item,newForm,newItem,obj);
                            break;
                        /*                        case 'ordOfferInstFeeInfos':
                                                    newForm=requestUtil.buildOrdOfferInstFeeInfos(tableName,item,newForm,newItem,Form);
                                                    break;*/
                        /**lvxx  添加了ordProdInsts  acctItems  */
                        case 'ordProdInsts' || 'ORD_PROD_INST':
                            newItem[colId] = item.value+'';
                            newForm=requestUtil.buildOrdProdInsts(tableName,item,newForm,newItem,obj);
                            break;
                        case 'acctItems' || 'ACCT_ITEM':
                            newItem[colId] = item.value+'';
                            
                            newForm=requestUtil.buildAcctItems(Form,tableName,item,newForm,newItem,obj,colId) ;
                            break;
                    }
                }else if(item.colType=='T'){
                    // 纵表
                    Object.assign(newItem,{
                        attrId:item.colId,
                        attrCd:item.fieldId,
                        roleCd:item.attrRoleCd,
                        attrVal:item.value
                    });
                    switch (tableName){
                        case 'orderTaskAttrs' || 'ORDER_TASK_ATTR':
                            newItem=requestUtil.buildOrderTaskAttrs(newItem,obj);
                            break;
                        case 'ordProdInstAttrs' || 'ORD_PROD_INST_ATTR':
                            newItem = requestUtil.buildProdInstAttrs(newItem,obj);
                            break;
                        case 'orderItemAttrs' || 'ORDER_ITEM_ATTR':
                            newItem = requestUtil.buildorderItemAttrs(newItem,obj);
                            break;
                    }
                    if(newForm[tableName]){
                        newForm[tableName].push(newItem);
                    }else{
                        newForm[tableName]=[]
                        newForm[tableName].push(newItem);
                    }
                }
            }
        }
        // 
        return newForm
    },
    /**
     * 月租费横表
     */
    buildOrdOfferInstFeeInfos: (tableName,item,newForm,newItem,Form) =>{
        if(item.colId == 'AMOUNT'){
            let newArr = Object.values(Form).filter((item) => {
                return item.tableName == 'PROD_OFFER_FEE_INFO' && item.colId !== 'AMOUNT'
            });
            let mapArr = '';
            mapArr = item.relIds.split(";");
            newItem.acctItemTypeId = item.acctItemId;
            newItem.amount = item.value+'';
            newItem.acctTypeCd = item.attrRoleCd;
            newItem.odrOfferInstFeeAttrs = requestUtil.buildProdInstAttr(Form, item.attrRoleCd,'PROD_OFFER_FEE_ATTR',item.custId);//PROD_OFFER_FEE_ATTR
            newItem = requestUtil.getArr2(mapArr, newArr, newItem);
            if(newForm[tableName]){
                Object.assign(newForm[tableName][0],newItem);
            }else{
                newForm[tableName]=[]
                newForm[tableName].push(newItem);
            }
        }
        return newForm;
    },
    getArr2: (mapArr, newArr, obj) => {
        for (var i = 0; i < mapArr.length; i++) {
            for (var j = 0; j < newArr.length; j++) {
                let id = newArr[j].id.split('-')[1];
                if (mapArr[i].includes(id)) {
                    obj[requestUtil.toHump(newArr[j].colId)] = newArr[j].value;
                }
            }
        }
        return obj;
    },
    /**
     * 订单任务横表
     */
    buildOrderTasks: (tableName,newForm,newItem,obj) => {
        let{orderTaskId,taskId,custSoNumber,orderItemSeq} = obj,
            actType='MOD',
            oprOrgId=obj.orgId,
            /**lvxx  添加了两个参数 userId userName   此项需要判断 页面是否存在 这两个属性 存在才放入报文中  */
            userId =obj.userId,
            userName =obj.userName;
        if(newForm[tableName]){
            Object.assign(newForm[tableName][0],newItem);
            // 
        }else{
            newItem = {orderTaskId,actType,oprOrgId,taskId,custSoNumber,orderItemSeq,userId,userName,...newItem};
            newForm[tableName]=[];
            newForm[tableName].push(newItem);
        }
        return newForm;
    },
    /**
     * 订单任务横表
     */
    buildOrderItems : (tableName,newForm,newItem,obj) => {
        let{orderItemId,custSoNumber,orderItemSeq} = obj,
            actType='MOD',
            oprOrgId=obj.orgId,
            /**lvxx  添加了两个参数 userId userName   此项需要判断 页面是否存在 这两个属性 存在才放入报文中  */
            userId =obj.userId,
            userName =obj.userName;
        if(newForm[tableName]){
            Object.assign(newForm[tableName][0],newItem);
            // 
        }else{
            newItem = {orderItemId,actType,oprOrgId,custSoNumber,orderItemSeq,userId,userName,...newItem};
            newForm[tableName]=[];
            newForm[tableName].push(newItem);
        }
        return newForm;
    },
    /**
     * acctItemOrgs 横表
     */
    buildAcctItemOrgs: (tableName,item,newForm,newItem,obj) => {
        let{orderItemId} = obj,
            actType='MOD',
            chargeOrgCd= item.attrRoleCd;
        if(newForm[tableName]){
            newForm[tableName].forEach((item,index)=>{
                if(item.attrRoleCd == chargeOrgCd) {
                    Object.assign(newForm[tableName][index],newItem);
                } else {
                    newItem = {orderItemId,actType,chargeOrgCd,...newItem};
                    newForm[tableName] = [];
                    newForm[tableName].push(newItem);
                }
            });
        }else{
            newItem = {orderItemId,actType,chargeOrgCd,...newItem};
            newForm[tableName]=[];
            newForm[tableName].push(newItem);
        }

        return newForm;
    },
    buildAcctItem: (res, attrRoleCd,custId) => {//一次费帐目类型标识分组
        let acctItems = [];
        res = Object.values(res);
        let newArr = res.filter((item) => {
            return item.tableName == 'ACCT_ITEM' && item.colId !== 'AMOUNT'
        })
        let newMount = res.filter((item) => {
            return item.tableName == 'ACCT_ITEM' && item.colId == 'AMOUNT'
        })
        newMount.forEach((item) => {
            let mapArr = '';
            let obj = {};
            if (item.attrRoleCd == attrRoleCd) {
                mapArr = item.relIds.split(";")
                obj.acctItemTypeId = item.ACCT_ITEM_ID;
                obj.amount = item.value+'';
                obj.custId = custId;
                obj.acctTypeCd = item.attrRoleCd == item.attrRoleCd ? item.attrRoleCd : '0';
                acctItems = requestUtil.getArr(mapArr, newArr, obj, acctItems);
            }
        });
        return requestUtil.mergeObject(acctItems,'acctItemTypeId');
    },
    /**
     * 过程实例横表
     */
    buildOrdProdInsts: (tableName,item,newForm,newItem,obj) => {

        /** 此处需要重新 考虑批量问题  */
        let{procProdInstId0,prodInstId0,procProdInstId100,prodInstId100,procProdInstId200,prodInstId200,custSoNumber} = obj,
            actType='MOD',
            roleCd= item.attrRoleCd,
            rowId="",
            prodInstId="";
        // 
        if(roleCd=="100"){
            rowId=procProdInstId100;
            prodInstId=prodInstId100;
        }else if(roleCd=="200"){
            rowId=procProdInstId200;
            prodInstId=prodInstId200;
        }else{
            rowId=procProdInstId0;
            prodInstId=prodInstId0;
        }
        if(newForm[tableName]){
            newForm[tableName].forEach((item,index)=>{
                if(item.roleCd == attrRoleCd) {
                    Object.assign(newForm[tableName][index],newItem);
                } else {
                    newItem = {rowId,prodInstId,custSoNumber,actType,roleCd,...newItem};
                    newForm[tableName].push(newItem);
                }
            });
        }else{
            newItem = {rowId,prodInstId,custSoNumber,actType,roleCd,...newItem};
            newForm[tableName]=[];
            newForm[tableName].push(newItem);
        }
        return newForm;
    },
    /**
     * 一次费横表  buildAcctItems(tableName,item,newForm,newItem,Form);
     */
    buildAcctItems: (Form,tableName,item,newForm,newItem,obj,colId) => {
        let{orderItemId,custOrderId} = obj,
            actType='MOD',
            acctTypeCd= item.attrRoleCd,
            acctItemTypeId= item.acctItemId;
        if(newForm[tableName]){
            if(colId =="AMOUNT"||colId=="amount"){
                /**   费用上需要挂载 币种 */
                for(var key in Form) {
                    let attrobj = Form[key];
                    let relIds =true;
                    if(item.hasOwnProperty("relIds")){
                        if(item.relIds.indexOf(attrobj.name)<0){
                            relIds =false;
                        }
                    }
                    if(attrobj.colId == "CURRENCY" && relIds && attrobj.attrRoleCd == acctTypeCd) {
                        newItem["currency"] =  attrobj.value;
                    }
                    if(attrobj.colId == "FIX_ONCE_FLAG" && attrobj.attrRoleCd == acctTypeCd) {
                        newItem["fixOnceFlag"] = attrobj.value;
                    }
                };
                newItem = {orderItemId,custOrderId,acctItemTypeId,actType,acctTypeCd,...newItem};
                if(obj.taskId=="TACHE00130001"||obj.taskId=="TACHE00130002" ){
                    newItem.actType='ADD';
                    var onceStage ="10";
                    var fixOnceFlag= "1";
                    var statusCd = "1000";
                    var acctItemOrgId = obj.acctItemOrgId+'';
                    newItem = {acctItemOrgId,statusCd,onceStage,fixOnceFlag,...newItem};
                }
                newForm[tableName].push(newItem);
            }
        }else{
            if(colId=="AMOUNT"||colId =="amount"){
                /**   费用上需要挂载 币种 */
                for(var key in Form) {
                    let attrobj = Form[key];
                    let relIds =true;
                    if(item.hasOwnProperty("relIds")){
                        if(item.relIds.indexOf(attrobj.name)<0){
                            relIds =false;
                        }
                    }
                    if(attrobj.colId == "CURRENCY" && relIds && attrobj.attrRoleCd == acctTypeCd) {
                        newItem["currency"] = attrobj.attrValue;
                    }
                    if(attrobj.colId == "FIX_ONCE_FLAG" && attrobj.attrRoleCd == acctTypeCd) {
                        newItem["fixOnceFlag"] = attrobj.attrValue;
                    }
                };
                newItem = {orderItemId,custOrderId,acctItemTypeId,actType,acctTypeCd,...newItem};
                if(obj.taskId=="TACHE00130001"||obj.taskId=="TACHE00130002" ){
                    newItem.actType='ADD';
                    var onceStage ="10";
                    var fixOnceFlag= "1";
                    var statusCd = "1000";
                    var acctItemOrgId = obj.acctItemOrgId+'';
                    newItem = {acctItemOrgId,statusCd,onceStage,fixOnceFlag,...newItem};
                }
                newForm[tableName]=[];
                newForm[tableName].push(newItem);
            }
        }
        return newForm;
    },
    /**
     * prodInstAttrs 纵表
     */
    buildProdInstAttrs: (newItem,obj) => {
        /**lvxx    添加 100 200实例 并加了区分         */
        let{prodInstId0,procProdInstId0,prodInstId100,procProdInstId100,prodInstId200,procProdInstId200} = obj,
            actType='MOD',
            roleCd= newItem.attrCd;
        if(newItem.attrRoleCd=="100"){
            return {prodInstId:prodInstId100,actType,rowId:procProdInstId100,...newItem};
        }else if(newItem.attrRoleCd=="200"){
            return {prodInstId:prodInstId100,actType,rowId:procProdInstId200,...newItem};
        }else{
            return {prodInstId:prodInstId0,actType,rowId:procProdInstId0,...newItem};
        }

    },
    /**
     * 订单任务纵表
     */
    buildOrderTaskAttrs: (newItem,obj) => {
        let{orderTaskId,taskId,custSoNumber,orderItemSeq} = obj,
            actType='MOD',
            oprOrgId=obj.orgId;
        newItem = {orderTaskId,actType,oprOrgId,taskId,custSoNumber,orderItemSeq,...newItem};
        return newItem;
    },
    /**
     * 订单属性表 纵表
     */
    buildorderItemAttrs: (newItem,obj) => {
        let{orderItemId,custSoNumber,orderItemSeq} = obj,
            actType='MOD',
            oprOrgId=obj.orgId;
        newItem = {orderItemId,actType,oprOrgId,custSoNumber,orderItemSeq,...newItem};
        return newItem;
    },
    /**
     * 资费信息
     */
    mergeObject: (arr, key) => {//将数组中特定属性相同的对象合并
        var intype = {};
        var newJson = [];
        arr.forEach((item) => {
            if (intype[item[key]]) {
                Object.assign(newJson[intype[item[key]] - 1], item);
            } else {
                newJson.push(item);
                intype[item[key]] = newJson.length;
            }
        });
        return newJson;
    },
    getArr: (mapArr, newArr, obj, acctItems) => {
        for (var i = 0; i < mapArr.length; i++) {
            let obj1 = {};
            for (var j = 0; j < newArr.length; j++) {
                let id = newArr[j].id.split('-')[1];
                if (mapArr[i].includes(id)) {
                    obj1[requestUtil.toHump(newArr[j].colId)] = newArr[j].value;
                }
            }
            var obj2 = { ...obj, ...obj1 }
            acctItems.push(obj2);
        }
        return acctItems;
    },
    /**
     * 大写下划线转驼峰
     */
    toHump: (name) => {
        var tfname = '';
        var array = name.split('');
        for(var i=0;i<array.length;i++){
            if(i==array.length-1) {tfname=name;break}
            let v = array[i],
                v2 = array[i+1];
            if(/^[A-Z]+$/.test(v)){
                if(/^[A-Z]+$/.test(v2)){
                    let n = name.toLowerCase(name);
                    tfname =  n.replace(/_(\w)/g, function(all, letter){
                        return letter.toUpperCase();
                    });
                    break;
                }
            }
        };
        return tfname
    }
};
export default requestUtil
